<?php
namespace app\index\controller;

use think\Request;

class Balance extends Common{

	//货款充值首页
	public function hk(){
		$response = \ServerHelper::post('recharge_product_index',['agentid'=>AGENT_ID]);
		$data = $this->J($response);
		return view('hk_index',$data['data']);
	}
	
	//货款充值进入选择支付方式阶段
	public function hk_recharge(){
		$request = Request::instance();
		//选择支付方式
		$param = [
			'agentid'=>AGENT_ID,
			'amount' => $request->param('amount',0,'floatval'),
		];
		$response = \ServerHelper::post('recharge_product_show',$param);
		$data = $this->J($response);
		return view('hk_recharge_paytype',$data['data']);
	}
	
	//货款消费记录
	public function hk_record(){
		$request = Request::instance();
		$datatype = $request->param('datatype');
		$page = $request->param('page');
		
		$response = \ServerHelper::post('recharge_product_index',['agentid'=>AGENT_ID]);
		$data = $this->J($response);
		$type = Request::instance()->param('type','','trim');
		if($type == 'recharge'){ //or use
			$list = $data['data']['RechargeCashlist'];
			$title = '货款充值记录';
			if($datatype == 'json'){
				for($i=0;$i<count($list);$i++){
					$list[$i]['ChargeAmount'] = number_format($list[$i]['ChargeAmount'],2);
				}
			}
		}else{
			$list = $data['data']['UseCashlist'];
			$title = '货款消费记录';
			if($datatype == 'json'){
				for($i=0;$i<count($list);$i++){
					$list[$i]['ChangeAmount'] = number_format($list[$i]['ChangeAmount'],2);
				}
			}
		}
		
		$typeArr = [
			['type' => 'recharge','typeName' => '收入'],
			['type' => 'use','typeName' => '支出']
		];
		
		if($datatype == 'json'){
			ajax('success',true,[
				'list'=>$list,
				'page'=>$page,
			]);
		}else{
			return view('',['list'=>$list,'title'=>$title,'type'=>$type,'typeArr'=>$typeArr]);
		}
	}
	
	//现金充值首页
	public function xj(){
		$response = \ServerHelper::post('recharge_cach_index',['agentid'=>AGENT_ID]);
		$data = $this->J($response);
		return view('xj_index',$data['data']);
	}
	
	//现金充值进入选择支付方式阶段
	public function xj_recharge(){
		$request = Request::instance();
		
		//选择支付方式
		$param = [
			'agentid'=>AGENT_ID,
			'amount' => $request->param('amount',0,'floatval'),
		];
		$response = \ServerHelper::post('recharge_cach_show',$param);
		$data = $this->J($response);
		return view('xj_recharge_paytype',$data['data']);
	}
	
	//现金消费记录
	public function xj_record(){
		
		$response = \ServerHelper::post('recharge_cach_index',['agentid'=>AGENT_ID]);
		$data = $this->J($response);
		$type = Request::instance()->param('type','cz');
		if($type == 'cz'){
			$list = $data['data']['Cashlist'];
			$title = '现金充值记录';
		}else if($type == 'xf'){
			$list = $data['data']['UseCashlist'];
			$title = '现金消费记录';
		}else if($type == 'sr'){
			$list = $data['data']['JCCashlist'];
			$title = '现金收入记录';
		}else if($type == 'tx'){
			$list = $data['data']['RrawList'];
			$title = '现金提现记录';
		}else if($type == 'jl'){
			$list = $data['data']['RewardList'];
			$title = '现金提现记录';
		}else{
			$this->error('参数错误');
		}
		
		return view('',[
			'list'=>$list,
			'title'=>$title,
			'type'=>$type,
			'typeArr'=>[
				['type' => 'cz','typeName'=>'充值'],
				['type' => 'xf','typeName'=>'消费'],
				['type' => 'sr','typeName'=>'收入'],
				['type' => 'tx','typeName'=>'提现'],
				['type' => 'jl','typeName'=>'奖励'],
			],
		]);
	}

	
	//①线下支付
	public function xxpay(){
		$ordertype = Request::instance()->param('ordertype'); //订单类型(付哪一个步骤)
		$paytype = 1;
		
		//真实打款金额
		$realamount = Request::instance()->param('realamount',0,'floatval'); 
		
		if($ordertype  == ''){
			$this->error('参数错误');
		}
		if($paytype == ''){
			$this->error('参数错误');
		}
			
		if(Request::instance()->isPost()){
			
			$postdata = [
				'agentid' => AGENT_ID,
				'ordertype' => $ordertype,
				'paytype' => $paytype,
				'amount' => $realamount,
				'repaccounttype' => Request::instance()->param('repaccounttype'),
				'repaccount' => Request::instance()->param('repaccount'),
				'remark' => Request::instance()->param('remark'),
			];
			$reppicture = Request::instance()->param('reppicture');
			if($postdata['repaccounttype'] == '' || $postdata['repaccount'] == ''){
				$this->error('请选择收款人账户');
			}
			$arr = explode(',',$reppicture);
			if(count($arr) < 1 || substr($arr[0],0,4) != 'http'){
				$this->error('请上传打款凭证');
			}
			$postdata['reppicture'] = json_encode($arr);
			
			__log(json_encode($postdata),'ttttt');
			$response = \ServerHelper::post('recharge_xxpay_do',$postdata);

			$response = $this->J($response);
			if($response['flag']){
				$url = $ordertype == 'orderamount' ? url('Balance/hk') : url('Balance/xj');
				$this->success('提交成功，请等待审核',$url);
			}else{
				$this->error($response['msg']);
			}
			
		}else{
		
			$request = [
				'agentid' => AGENT_ID,
				'ordertype' => $ordertype,
				'amount' => $realamount
			];
			
			$response = \ServerHelper::post('recharge_xxpay_show',$request);
			
			$response = $this->J($response);
			if($response['flag'] == 0){
				$this->error($response['msg']);
			}
			//选择数据
			$accountType = [];
			$accountTypeFull = [];
			if(isset($response['data']['repaccount']['wechat'])){
				$accountType[] = [
					'id'=>$response['data']['repaccount']['wechat']['accountid'],
					'value'=>$response['data']['repaccount']['wechat']['accountname'],
				];
				$accountTypeFull['wechat'] = [
					'id'=>$response['data']['repaccount']['wechat']['accountid'],
					'value'=>$response['data']['repaccount']['wechat']['accountname'],
				];
			}
			if(isset($response['data']['repaccount']['alipay'])){
				$accountType[] = [
					'id'=>$response['data']['repaccount']['alipay']['accountid'],
					'value'=>$response['data']['repaccount']['alipay']['accountname'],
				];
				$accountTypeFull['alipay'] = [
					'id'=>$response['data']['repaccount']['alipay']['accountid'],
					'value'=>$response['data']['repaccount']['alipay']['accountname'],
				];
			}
			if(isset($response['data']['repaccount']['bank'])){
				$accountType[] = [
					'id'=>$response['data']['repaccount']['bank']['accountid'],
					'value'=>$response['data']['repaccount']['bank']['accountname'],
				];
				$accountTypeFull['bank'] = [
					'id'=>$response['data']['repaccount']['bank']['accountid'],
					'value'=>$response['data']['repaccount']['bank']['accountname'],
				];
			}
			
			return view('',[
				'data' => $response['data'],
				'ordertype' => $ordertype,
				'paytype' => $paytype,
				'realamount' => $realamount,
				'accounttype' => json_encode($accountType,JSON_UNESCAPED_UNICODE),
				'accounttypefull' => json_encode($accountTypeFull,JSON_UNESCAPED_UNICODE)
			]);
		}

	}
	

	//②微信支付
	public function wepay(){
		
		//现金余额、货款余额
		$ordertype = Request::instance()->param('ordertype'); //订单类型(付哪一个步骤)
		$amount = Request::instance()->param('realamount'); 
		$paytype = PAYTYPE_WEIXIN; //支付类型、微信
		
		if($ordertype  == ''){
			$this->error('参数错误');
		}
		//参数待确定
		
		//拉取支付信息
		$request = [
			'agentid' => AGENT_ID,
			'ordertype' => $ordertype,
			'paytype' => $paytype,
			'amount' => $amount,
		];
		$payinfo = \ServerHelper::post('recharge_wepay_show',$request);
		$payinfo = $this->J($payinfo);
		if($payinfo['flag'] != 1){ $this->error('支付发起失败'); }

		__log('orderid：'.$payinfo['data']['orderid'].'，payid：'.$payinfo['data']['payid'],'tmp' );
    
		$param = [
			'body' => $payinfo['data']['products'],
			'orderid' => $payinfo['data']['orderid'],
			'pay_no' => $payinfo['data']['payid'],
			'price' => intval($payinfo['data']['amount']), //价格(单位：分)
			'notify_url' => Request::instance()->domain() . url('Rechargewepay/notify_url'),
		];
		
		$url = url('Rechargewepay/index') . '?' . http_build_query($param);
		
		$this->redirect($url);
	}
	
}
